package com.nforce.linremote.networking;

import com.nforce.linremote.model.Server;
import com.nforce.linremote.serverapi.Library;

public class SessionUtils {

    private static String sid;
    private static Server server;

    public static boolean initSession(String sid, Server server) {
	if (isSessionActive()) {
	    return false;
	}
	SessionUtils.sid = sid;
	SessionUtils.server = server;

	return true;
    }

    public static Server getServer() {
	return server;
    }

    public static String getSid() {
	return sid;
    }

    public static boolean isAnotherServerInCache(Server s) {
	if (sid != null && server != null) {
	    return !s.equals(server);
	}
	return false;
    }

    public static boolean isSessionActive() {
	if (sid != null && server != null) {
	    if (Library.isSessionActive(server, sid)) {
		return true;
	    } else {
		sid = null;
		server = null;
	    }
	}
	return false;
    }

    public static boolean terminateSession() {
	if (!isSessionActive()) {
	    return false;
	}
	Library.disconnect(getServer());
	sid = null;
	server = null;
	return true;
    }
}
